file(GLOB SOURCES "*.cpp")
set(PYTHON_EXECUTABLE "python3")
execute_process(
    COMMAND ${PYTHON_EXECUTABLE} -c "import sys; print([s for s in sys.path if 'packages' in s][0])"
    OUTPUT_VARIABLE PYTHON_SYS_PATH
)
string(STRIP ${PYTHON_SYS_PATH} PYTHON_SYS_PATH)

if("${PYTHON_SYS_PATH}" STREQUAL "")
    message(FATAL_ERROR "Python path is empty! Please check the python env.")
endif()

include_directories("../mooncake-store/include")
include_directories("../mooncake-store/include/cachelib_memory_allocator")

include_directories("../mooncake-store/include/cachelib_memory_allocator/include")
include_directories("../mooncake-store/include/cachelib_memory_allocator/fake_include")

include_directories("/usr/include/jsoncpp")

pybind11_add_module(mooncake_vllm_adaptor ${SOURCES} ${CACHE_ALLOCATOR_SOURCES}
    vllm/vllm_adaptor.cpp 
    vllm/distributed_object_store.cpp
)

set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)

set_target_properties(mooncake_vllm_adaptor PROPERTIES
    INSTALL_RPATH "$ORIGIN/lib_so"
    BUILD_RPATH "$ORIGIN/lib_so"
    SKIP_BUILD_RPATH FALSE
)
 

target_link_libraries(mooncake_vllm_adaptor PUBLIC 
    transfer_engine 
    glog::glog
    gflags::gflags
    mooncake_store 
    cachelib_memory_allocator
)
message("${PYTHON_SYS_PATH}")
install(TARGETS mooncake_vllm_adaptor DESTINATION ${PYTHON_SYS_PATH}/)

set(PYTHON_PACKAGE_NAME "mooncake")
pybind11_add_module(engine ${SOURCES} ${CACHE_ALLOCATOR_SOURCES}
    transfer_engine/transfer_engine_py.cpp 
)
target_link_libraries(engine PUBLIC 
    transfer_engine 
    glog::glog
    gflags::gflags
)
pybind11_add_module(store ${SOURCES} ${CACHE_ALLOCATOR_SOURCES}
    store/store_py.cpp 
)
target_link_libraries(store PUBLIC 
    transfer_engine 
    glog::glog
    gflags::gflags
    mooncake_store 
    cachelib_memory_allocator
)
message("${PYTHON_SYS_PATH}")

file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${PYTHON_PACKAGE_NAME}/__init__.py
    "# Auto-generated by CMake\n"
)

install(
    DIRECTORY
        ${CMAKE_CURRENT_BINARY_DIR}/${PYTHON_PACKAGE_NAME}/
    DESTINATION
        ${PYTHON_SYS_PATH}/${PYTHON_PACKAGE_NAME}
)

install(
    CODE "
        execute_process(COMMAND chmod 766 \"${PYTHON_SYS_PATH}/${PYTHON_PACKAGE_NAME}\")
        execute_process(COMMAND chmod 766 \"${PYTHON_SYS_PATH}/${PYTHON_PACKAGE_NAME}/__init__.py\")
    "
)
install(TARGETS store DESTINATION ${PYTHON_SYS_PATH}/${PYTHON_PACKAGE_NAME})
install(TARGETS engine DESTINATION ${PYTHON_SYS_PATH}/${PYTHON_PACKAGE_NAME})
